package com.wuneng.ircontrol.myUtil.blutetooth;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import com.wuneng.ircontrol.R;


//mResoult[0]//甲烷
//mResoult[1] //二氧化碳
//mResoult[2] //一氧化碳
//mResoult[3] //硫化氢
//mResoult[4]//氧气
//mResoult[5]//温度
//mResoult[6] //电池电压
//mResoult[7] //随机ID

public class PDAManager {

	private static PDAManager mManager;
	// for data
	public static HashMap<String, String> BluetoothData = new HashMap<String, String>();
	public static String[] mResoult = { "X", "X", "X", "X", "X", "X", "X", "X" };// 解析完的数据
	public static boolean[] mResoultState = new boolean[8];// 数据状态
	public static String mReceivedData;
	public static int recvCount;// 接收到的帧数
	public static boolean recvState;// 接收状态

	// test
	public static ByteArrayOutputStream myoutputstream;
	private static SharedPreferences mPreference;
	private static String PreferenceName = "__pda__";

	private static DataOutputStream dos;
	public final static String FNAME = "_Log.txt";
	public static String name;
	public static InputStream in;
	public static String parentPath;
	public static Context c;

	private static TimeoutThread mTimeoutThread;
	private MyTimerTask mTimerTask;
	public int MyTimeOut = 1;
	Timer t;

	/**
	 * PDAManager.mResoult[0].substring(0, PDAManager.mResoult[0].length()-1);
	 * 接收帧解析
	 * 
	 * @param str
	 */
	public static void parserData(String str, Context context) {
			BluetoothData.put("key", str);
			Log.i("111", "parserData---" + str);
			int id = 0;
			if (str.length() == 4) {
				Log.i("111", "str.length() == 4");
				str = "00";
			} else if (str.length() == 12) {
				str = "1100" + str;
				Log.i("111", "str.length() == 12------and str" + str);
			}
			try {
				id = Integer.parseInt(str.substring(0, 2));
					Log.i("111", "id=" + id);
					int value;
					// BluetoothData =new HashMap<String, String>();
					String[] datas = context.getResources().getStringArray(R.array.Ch4Items);
					if (id == 11) {
						recvCount++;
						recvState = true;
						str = str.substring(2, 14);
						System.out.println(id);
						System.out.println(str);
						if (str.substring(0, 4).equals("a710")) {
							BluetoothData.put("甲烷", "X");
							mResoult[0] = "X";
							System.out.println("甲烷" + mResoult[0]);
						} else {
							value = CheckValue(str.substring(0, 4));
							mResoult[0] = CheckZero(value / 100.00 + "");// 甲烷
							PDAManager.mResoultState[0] = true;
							System.out.println("甲烷" + mResoult[0]);

							BluetoothData.put(datas[0], mResoult[0]);
						}

						if (str.substring(4, 8).equals("a710")) {
							mResoult[1] = "X";
							System.out.println("二氧化碳" + mResoult[1]);
						} else {
							value = CheckValue(str.substring(4, 8));
							mResoult[1] = CheckZero(value / 100.00 + "");// 二氧化碳
							PDAManager.mResoultState[1] = true;
							System.out.println("二氧化碳" + mResoult[1]);

							BluetoothData.put(datas[1], mResoult[1]);
						}
						if (str.substring(8, 12).equals("a710")) {
							mResoult[2] = "X";
							System.out.println("一氧化碳" + mResoult[2]);
						} else {
							value = CheckValue(str.substring(8, 12));
							mResoult[2] = CheckZero(value / 1.00 + "");// 一氧化碳
							if (value > 100) {
								mResoult[2] = "0.00";
							}
							PDAManager.mResoultState[2] = true;
							System.out.println("一氧化碳" + mResoult[2]);
							BluetoothData.put(datas[2], mResoult[2]);
						}

					} else if (id == 21) {
						recvCount++;
						recvState = true;
					str = str.substring(2, 14);
					System.out.println(id);
					System.out.println(str);
					if (str.substring(0, 4).equals("a710")) {
						mResoult[3] = "X";
						System.out.println("硫化氢" + mResoult[3]);
					} else {
						value = CheckValue(str.substring(0, 4));
						mResoult[3] = CheckZero(value / 10.00 + "");// 硫化氢
						PDAManager.mResoultState[3] = true;
						System.out.println("硫化氢" + mResoult[3]);

						BluetoothData.put(datas[3], mResoult[3]);
					}

					if (str.substring(4, 8).equals("a710")) {
						mResoult[4] = "X";
						System.out.println("氧气" + mResoult[4]);
					} else {
						value = CheckValue(str.substring(4, 8));
						mResoult[4] = CheckZero(value / 10.00 + "");// 氧气
						PDAManager.mResoultState[4] = true;
						System.out.println("氧气" + mResoult[4]);

						BluetoothData.put(datas[4], mResoult[4]);
					}
					if (str.substring(8, 12).equals("a710")) {
						mResoult[5] = "X";
						System.out.println("温度" + mResoult[5]);
					} else {
						value = CheckValue(str.substring(8, 12));
						mResoult[5] = CheckZero(value / 10.00 + "");// 温度
						PDAManager.mResoultState[5] = true;
						System.out.println("温度" + mResoult[5]);

						BluetoothData.put(datas[5], mResoult[5]);
					}
				} else if (id == 31) {
					recvCount++;
					recvState = true;
					str = str.substring(2, 14);
					System.out.println(id);
					System.out.println(str);
					if (str.substring(0, 4).equals("a710")) {
						mResoult[6] = "X";
					} else {
						value = CheckValue(str.substring(0, 4));
						mResoult[6] = CheckZero(value / 100.00 + "");// 电池电压

						System.out.println("电压" + mResoult[6]);
						BluetoothData.put(datas[6], mResoult[6]);
					}

					if (str.substring(4, 12).equals("a710")) {
						mResoult[7] = "X";
					} else {
						value = CheckValue(str.substring(4, 12));
						mResoult[7] = value + "";// 随机值
						System.out.println("？？？" + mResoult[7]);
					}
				}
			} catch (Exception ex) {
				Log.i("PDAManager", ex.getMessage());
			}

	

	}

	/**
	 * 如果第一位是1，则为负数
	 */

	public static int CheckValue(String value) {
		String a, b;
		int c = Integer.parseInt(value.substring(0, 1), 16);
		if (c > 7) {
			b = Integer.toString(c - 8);
			b = b + value.substring(1, 4);
			a = "-";
			a = a + b;
		} else
			a = value;
		int TrueValue = Integer.parseInt(a, 16);

		return TrueValue;
	}

	/**
	 * 补零，补2个零
	 */
	public static String CheckZero(String value) {
		String s = value;
		int i = s.indexOf(".");
		int j = s.length();
		if (j - (i + 1) < 2) {
			s = s + "0";
		}

		return s;
	}

	/**
	 * byte数组 to 十六进制字符串
	 *
	 */
	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	public static float getString(String str, int i) {
		float string = (float) 0.00;
		try {
			// if(str.contains("."))
			// {
			// if(i==0)
			// {
			// string = Float.parseFloat(str.substring(0,str.indexOf(".")-1));
			// }
			// else {
			// string = Float.parseFloat(str.substring(0,str.indexOf(".")+i+1));
			// }
			//
			// }

			string = Float.parseFloat(str);
		} catch (Exception e) {
			// TODO: handle exception
		}

		return string;
	}

	public void checktimeoutThread() {
		timeOutCheck();
		ReSetState();
		if (mTimeoutThread != null) {
			mTimeoutThread = null;

			mTimeoutThread = new TimeoutThread();
			mTimeoutThread.start();
		}

	}

	private class TimeoutThread extends Thread {
		public void run() {

			if (t != null)
				t.cancel();

			if (mTimerTask != null)
				mTimerTask = null;

			t = new Timer();
			mTimerTask = new MyTimerTask();
			MyTimeOut = (int) (CommonData.TIMEOUTDATA * 60 * 1000);
			t.schedule(mTimerTask, MyTimeOut, MyTimeOut);
		}
	}

	class MyTimerTask extends TimerTask {

		public void run() {
			// TODO Auto-generated method stub
			if (MyTimeOut != (int) (CommonData.TIMEOUTDATA * 60 * 1000)) {

				if (t != null)
					t.cancel();

				if (mTimerTask != null)
					mTimerTask = null;

				t = new Timer();
				MyTimeOut = (int) (CommonData.TIMEOUTDATA * 60 * 1000);
				mTimerTask = new MyTimerTask();
				t.schedule(mTimerTask, MyTimeOut, MyTimeOut);
			}

			timeOutCheck();
			ReSetState();
		}
	}

	public static void timeOutCheck() {
		// TODO Auto-generated method stub

		if (mResoultState[0] == false)
			mResoult[0] = "X";
		if (mResoultState[1] == false)
			mResoult[1] = "X";
		if (mResoultState[2] == false)
			mResoult[2] = "X";
		if (mResoultState[3] == false)
			mResoult[3] = "X";
		if (mResoultState[4] == false)
			mResoult[4] = "X";
		if (mResoultState[5] == false)
			mResoult[5] = "X";
		if (mResoultState[6] == false)
			mResoult[6] = "X";
		if (mResoultState[7] == false)
			mResoult[7] = "X";

		if (recvState == false)
			mReceivedData = "X";
	}

	public static void ReSetValue() {
		// TODO Auto-generated method stub

		mResoult[0] = "X";// 甲烷

		mResoult[1] = "X";// 二氧化碳

		mResoult[2] = "X";// 一氧化碳

		mResoult[3] = "X";// 硫化氢

		mResoult[4] = "X";// 氧气

		mResoult[5] = "X";// 温度

		mResoult[6] = "X";// 电池电压

		mResoult[7] = "X";// 随机ID

		mReceivedData = "X";
	}

	/*
	 * 复位
	 */
	public static void ReSetState() {
		mResoultState[0] = false;
		mResoultState[1] = false;
		mResoultState[2] = false;
		mResoultState[3] = false;
		mResoultState[4] = false;
		mResoultState[5] = false;
		mResoultState[6] = false;
		mResoultState[7] = false;

		recvState = false;
	}

	public static boolean checkDataIsRight(String data, Context context){
		return !data.equals(context.getString(R.string.ble_error_data_1));
	}
}